﻿using Acme.SmartWMS.Location;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace Acme.SmartWMS.Inbound.PurchaseService
{
    public class PurchaseAppService:ApplicationService,IPurchaseAppService
    {
        /// <summary>
        /// 进货通知明细仓储
        /// </summary>
        private readonly IRepository<Purchase, int> _purchaseRepository;
        public PurchaseAppService(IRepository<Purchase, int> purchaseRepository)
        {
            _purchaseRepository = purchaseRepository;
        }
        /// <summary>
        /// 获取进货通知明细显示 查询 分页
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="notificationNo"></param>
        /// <param name="customerName"></param>
        /// <returns></returns>
        public async Task<PagedResult> GetPurchaseList(int pageIndex,int pageSize,string ? notificationNo,string ? customerName)
        {
            var predicate = PredicateBuilder.New<Purchase>(true);
            if (!string.IsNullOrEmpty(notificationNo))
            {
                predicate=predicate.And(x=>x.NotificationNo.Contains(notificationNo));
            }
            if (!string.IsNullOrEmpty(customerName))
            {
                predicate = predicate.And(x => x.CustomerName.Contains(customerName));
            }
            var dto = await _purchaseRepository.GetListAsync(predicate);
             PagedResult result = new PagedResult();
            result.PageCount = (int)Math.Ceiling((decimal)(dto.Count == 0 ? 1 : dto.Count) / pageSize);
            result.CurrentPage = pageIndex;
            result.PageSize = pageSize;
            result.RowCount=dto.Count;
            result.Queryable=dto.Skip((pageIndex-1)*pageSize).Take(pageSize).AsQueryable();
            return result;
        }
    }
}
